package Pages;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import selenium.WebDriverWrapper;
import utils.Log4Test;

import java.util.concurrent.TimeUnit;

/**
 * Created by Time to be on 08.07.2014.
 */
public class ProductsPage extends BasePage {

    private static final By toCopmare = By.xpath("//*[@id=\"head_banner_container\"]/div[3]/div/div[1]/div/div[1]/div/div[1]/div/div/div[3]/ul/li[1]/div/a");
    private static final By toCopmareUnclickable = By.xpath("//*[@id=\"head_banner_container\"]/div[3]/div/div[1]/div/div[1]/div/div[1]/div/div/div[3]/ul/li[1]/div");
    private String url;
    private WebDriverWait wait = new WebDriverWait(driver, 10);

    //*[@id="head_banner_container"]/div[3]/div/div[1]/div/div[1]/div/div[1]/div/div/div[3]/ul/li[1]/div/a
    //*[@id="head_banner_container"]/div[3]/div/div[1]/div/div[1]/div/div[1]/div/div/div[3]/ul/li[1]/div

    public ProductsPage(WebDriverWrapper driver) {
        super(driver);
    }

    public void toCompare(){
        //Actions builder = new Actions(driver);

        Log4Test.inMethod("toCompare");

        if (ableToClick()) {
            wait.until(ExpectedConditions.presenceOfElementLocated(toCopmareUnclickable));
        //    builder.moveToElement(driver.findElement(toCopmare));
            driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
            driver.findElement(toCopmare).click();
        }

    }

    private boolean ableToClick () {
        try {
            driver.findElement(toCopmare);
            return true;
        } catch (NoSuchElementException e) {
            Log4Test.error("BUG! Rerun test");
            return false;
        }
    }

}
